package com.ferryc.commons.entity;

import java.util.Date;

import org.hibernate.validator.constraints.Length;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.ferryc.commons.utils.StringUtil;
import com.ferryc.constant.CommonConstants;
import com.ferryc.constant.GlobalConstant;
import com.ferryc.constant.ValidatorConstant;
import com.ferryc.modules.sys.entity.SysUser;
import com.ferryc.modules.sys.util.DictUtils;
import com.ferryc.modules.sys.util.UserUtils;

@SuppressWarnings("serial")
public abstract class DataEntity<T> extends BaseEntity {

	/** 描述 */
	protected String description;
	/** 备注 */
	protected String remarks;
	/** 创建者 */
	protected SysUser createBy;
	/** 创建日期 */
	protected Date createDate;
	/** 更新者 */
	protected SysUser updateBy;
	/** 更新日期 */
	protected Date updateDate;
	/** 删除标记 0：正常；1：删除 */
	protected String delFlag;
	/** 创建用户名 */
	protected String createByUserName;
	/** 更新用户名 */
	protected String updateByUserName;
	/** 状态，1:启用,0:停用 */
	protected String status;
	/** 状态名 */
	protected String statusName;
	/** 模糊查询 字段 */
	protected String likeName;

	public DataEntity() {
		super();
		this.delFlag = GlobalConstant.DEL_FLAG_NORMAL;
		this.status = GlobalConstant.YES;
	}

	public DataEntity(String id) {
		super(id);
	}

	/**
	 * 插入之前执行方法，需要手动调用
	 */
	@Override
	public void preInsert() {
		// 不限制ID为UUID，调用setIsNewRecord()使用自定义ID
		if (!this.isNewRecord) {
			setId(StringUtil.getUUId());
		}
		SysUser user = UserUtils.getShiroUser();
		if (StringUtil.isNotBlank(user.getId())) {
			this.updateBy = user;
			this.createBy = user;
		}
		this.updateDate = new Date();
		this.createDate = this.updateDate;
	}

	/**
	 * 更新之前执行方法，需要手动调用
	 */
	@Override
	public void preUpdate() {
		SysUser user = UserUtils.getShiroUser();
		if (StringUtil.isNotBlank(user.getId())) {
			this.updateBy = user;
		}
		this.updateDate = new Date();
	}

	@Length(max = 255, message = ValidatorConstant.ERROR_DESCRIPTION)
	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	@Length(max = 255, message = ValidatorConstant.ERROR_REMARKS)
	public String getRemarks() {
		return remarks;
	}

	public void setRemarks(String remarks) {
		this.remarks = remarks;
	}

	@JsonIgnore
	public SysUser getCreateBy() {
		return createBy;
	}

	public void setCreateBy(SysUser createBy) {
		this.createBy = createBy;
	}

	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	@JsonIgnore
	public SysUser getUpdateBy() {
		return updateBy;
	}

	public void setUpdateBy(SysUser updateBy) {
		this.updateBy = updateBy;
	}

	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	public Date getUpdateDate() {
		return updateDate;
	}

	public void setUpdateDate(Date updateDate) {
		this.updateDate = updateDate;
	}

	@JsonIgnore
	@Length(min = 1, max = 1)
	public String getDelFlag() {
		return delFlag;
	}

	public void setDelFlag(String delFlag) {
		this.delFlag = delFlag;
	}

	public String getCreateByUserName() {
		return this.getCreateBy() != null ? this.getCreateBy().getRealName() : "";
	}

	public void setCreateByUserName(String createByUserName) {
		this.createByUserName = createByUserName;
	}

	public String getUpdateByUserName() {
		return this.getUpdateBy() != null ? this.getUpdateBy().getRealName() : "";
	}

	public void setUpdateByUserName(String updateByUserName) {
		this.updateByUserName = updateByUserName;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getStatusName() {
		String statusStr = DictUtils.getDictLabel(this.status, CommonConstants.YES_NO, "");
		String statusColor = GlobalConstant.YES.equals(this.status) ? "badge-primary" : "badge-danger";
		return "<span class='badge  " + statusColor + "'>" + statusStr + "</span>";
	}

	public void setStatusName(String statusName) {
		this.statusName = statusName;
	}

	public String getLikeName() {
		return likeName;
	}

	public void setLikeName(String likeName) {
		this.likeName = likeName;
	}

}
